//
//  RegisterViewController.m
//  UI16.真.豆瓣
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 Amydom. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "LoginView.h"
#import "MBProgressHUD.h"
#import "SingLeton.h"
#import "UserModel.h"
@interface RegisterViewController ()<MBProgressHUDDelegate>

@property (nonatomic , retain)RegisterView *regist;
@property (nonatomic ,retain)LoginView *loginV;
@property (nonatomic, retain)MBProgressHUD *hud;


@end

@implementation RegisterViewController

- (void)dealloc{
    [_myName release];
    [_mypassWord release];
    [_regist release];
    [super dealloc];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.regist = [[RegisterView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.regist];
    [_regist release];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(RightAction:)];
    
    
}



-(void)RightAction:(UIBarButtonItem *)btn{
    
    
    
    if (![self.regist.userField.text isEqualToString:@""] && ![self.regist.passwordField.text isEqualToString:@""] && ![self.regist.verifyPWField.text isEqualToString:@""] && [self.regist.passwordField.text isEqualToString:self.regist.verifyPWField.text]) {
        
        [[NSUserDefaults standardUserDefaults] setObject:self.regist.userField.text forKey:@"name"];
        [[NSUserDefaults standardUserDefaults] setObject:self.regist.passwordField.text forKey:@"password"];
        [[NSUserDefaults standardUserDefaults] setObject:self.regist.EmileField.text forKey:@"email"];
        [[NSUserDefaults standardUserDefaults] setObject:self.regist.contactField.text forKey:@"phoneNumber"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        self.myName = self.regist.userField.text;
        self.mypassWord = self.regist.passwordField.text;
        
    
       UserModel *user = [[UserModel alloc]init];
       user.UserName = self.regist.userField.text;
        user.passWord = self.regist.passwordField.text;
        user.Emile = self.regist.EmileField.text;
        user.contact = self.regist.contactField.text;
        
        [[SingLeton shareSingleton]insertUser:user];
        
        
        
        self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.hud.labelText = @"注册成功";
        self.hud.mode = MBProgressHUDModeText;
        self.hud.delegate = self;
        [self.hud hide:YES afterDelay:1];
        
    } else {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入完整" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        // 创建提示框按钮
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        // 将按钮放在提示框上
        [alertC addAction:action];
    }
    
}



- (void)hudWasHidden:(MBProgressHUD *)hud{
   
    [self.navigationController popViewControllerAnimated:YES];
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
